<?php
/**
 * Created by PhpStorm.
 * User: USER
 * Date: 2017/4/28
 * Time: 15:38
 */

namespace JsonRpc\Services\Order;


use JsonRpc\Models\SeOrder;
use JsonRpc\Models\SeOrderLine;
use JsonRpc\Models\SeOrderLineLog;
use JsonRpc\Services\BaseService;
use JsonRpc\Services\System\UserService;
use JsonRpc\Utils\Utils;

class OrderService extends BaseService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\Req";
    }

    //认购单转单生成订单
    public static function addOrder()
    {
        if(isset($params['id']) && $params['id']){
            //更新
            $model = SeOrder::find($params['id']);
            $model->updated_by = $params['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            //新增
            unset($params['id']);
            $model = new SeOrder();
            $model->client_id = $user['client_id'];
            $model->org_id = $user['org_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
            $model->sn = Utils::createSn($params['id']);
        }
        $model->req_id = $params['req_id'];
        $model->orderdate = $params['orderdate'];
        $model->orderdate = $params['orderdate'];
        $model->customer_id = $params['customer_id'];
        $model->customer_mobile = $params['customer_mobile'];
        $model->customer_name = $params['customer_name'];
//        $model->description = $params['description'];
        $model->warehouse_id = $params['warehouse_id'];
        $model->shipping_methods = $params['shipping_methods'];
        $model->shipping_address = $params['shipping_address'];
        $model->salesrep_id = $params['salesrep_id'];
        self::beginTransaction();
        $model->save();
        SeOrderLineLog::insert([
            'client_id' => $model->client_id,
            'org_id' => $model->org_id,
            'created_by' => $model->created_by,
            'updated_by' => $model->updated_by,
            'req_id' => $model->id,
            'content' => '认购单转订单成功，编号为：'.$model->sn,
        ]);
        self::commit();
        return $model->id;
    }

    //保存商品信息
    public static function saveOrderGoodsInfo($data)
    {

        if(isset($data['orderline_id']) && $data['orderline_id']){
            //更新
            $model = new SeOrderLine();
            $model->where('id',$data['orderline_id']);
            $model->updated_by = $data['curr_user_id'];
        }else{
            $user = UserService::getInfoById(['id'=>$data['curr_user_id']]);
            //新增
            $model = new SeOrderLine();
            $model->client_id = $user['client_id'];
            $model->org_id = $user['org_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }
        $model->reqline_id = $data['reqline_id'];  //认购商品id
        $model->order_id = $data['order_id'];  //订单id
        $model->goods_id = $data['goods_id'];
//        $model->req_id = $data['req_id'];
//            $model->goods_name = $data['goods_name'];
        $model->goods_price = $data['goods_price'];
        $model->goods_num = $data['order_num'];
        $model->line_amt = $data['order_num']*$data['goods_price'];
        $r = $model->save();

        return $r;
    }
}